package com.aiju.cash.ui.main.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.aiju.cash.R;
import com.aiju.cash.ui.good.adapter.TeamsAndHeaderAdapter;
import com.aiju.cash.ui.main.entity.BlotterList;
import com.aiju.utils.DateUtil;
import com.aiju.utils.L;
import com.timehop.stickyheadersrecyclerview.StickyRecyclerHeadersAdapter;

import java.util.ArrayList;
import java.util.List;

/**
 * @author: 佐为
 * @date: 2016-12-19 10:09
 * @version: V1.0
 * @Description: 描述
 */
public class BlotterAdater extends RecyclerView.Adapter<RecyclerView.ViewHolder> implements StickyRecyclerHeadersAdapter<RecyclerView.ViewHolder> {

    private List<BlotterList> categoryList;
    private List<BlotterList.Blotter> teamList = new ArrayList<>();
    private Context mContext;

    public BlotterAdater(Context context, List<BlotterList> categoryList) {
        this.mContext = context;
        setCategoryList(categoryList);
    }

    public void setCategoryList(List<BlotterList> d) {
        teamList.clear();
        this.categoryList = d;
        for (BlotterList b : categoryList) {
            teamList.addAll(b.getList());
        }
        notifyDataSetChanged();
    }

/*    */

    /**
     * 分类总价格
     *
     * @return
     *//*
    private String getCategoryPrice(BlotterList blotterList) {
        Double sum = 0d;
        for (BlotterList.Blotter blotter : blotterList.getList()) {
            sum += Double.valueOf(blotter.getPrice());
        }
        L.d("分类总价：" + blotterList.getList().size());
        return PriceUtil.formatDouble(sum);
    }*/
    public List<BlotterList> getCategoryList() {
        return categoryList;
    }

    public List<BlotterList.Blotter> getTeamList() {
        return teamList;
    }

    @Override
    public int getItemCount() {
        return teamList.size();
    }

    /**
     * 返回值相同会被默认为同一项
     *
     * @param position
     * @return
     */
    @Override
    public long getHeaderId(int position) {
        return getSortType(position);
    }

    //获取当前球队的类型
    public int getSortType(int position) {
        int sort = -1;
        int sum = 0;
        for (int i = 0; i < categoryList.size(); i++) {
            if (position >= sum) {
                sort++;
            } else {
                return sort;
            }
            sum += categoryList.get(i).getList().size();
        }
        return sort;
    }

    /**
     * ===================================================================================================
     * header的ViewHolder
     * ===================================================================================================
     */
    @Override
    public HeaderViewHolder onCreateHeaderViewHolder(ViewGroup viewGroup) {
        View view = LayoutInflater.from(viewGroup.getContext())
                .inflate(R.layout.item_blotter_head, viewGroup, false);
        return new HeaderViewHolder(view);
    }

    @Override
    public void onBindHeaderViewHolder(RecyclerView.ViewHolder viewHolder, int position) {
        View view = viewHolder.itemView;

        TextView dateTextView = (TextView) view.findViewById(R.id.tv_billing_date);
        dateTextView.setText(categoryList.get(getSortType(position)).getDate());

        TextView priceTsextView = (TextView) view.findViewById(R.id.tv_billing_price);
        priceTsextView.setText(categoryList.get(getSortType(position)).getSum());
    }

    private class HeaderViewHolder extends RecyclerView.ViewHolder {
        public HeaderViewHolder(View itemView) {
            super(itemView);
        }
    }

    /**
     * ==================================================================================================
     * 以下为contentViewHolder
     * ==================================================================================================
     */
    @Override
    public ContentViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.item_blotter, parent, false);

        return new ContentViewHolder(view);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
        ContentViewHolder viewHolder = (ContentViewHolder) holder;
        final BlotterList.Blotter blotter = teamList.get(position);

        L.d("流水：" + blotter.getPrice());
        if (!TextUtils.isEmpty(blotter.getPayType())) {
            if (blotter.getPayType().equals("0")) {//现金
                viewHolder.payTypeImageView.setImageResource(R.mipmap.icon_blotter_money);
            } else if (blotter.getPayType().equals("1")) {//支付宝
                viewHolder.payTypeImageView.setImageResource(R.mipmap.icon_blotter_zhifu);
            } else if (blotter.getPayType().equals("3")) {//qq
                viewHolder.payTypeImageView.setImageResource(R.mipmap.icon_blotter_qq);
            } else {// 2 微信
                viewHolder.payTypeImageView.setImageResource(R.mipmap.icon_blotter_wechat);
            }
        } else {
            viewHolder.payTypeImageView.setImageResource(R.mipmap.icon_blotter_refund);
        }

        viewHolder.dateTextView.setText(DateUtil.getWeekNumber(blotter.getCreateDate(), DateUtil.dateFormatYMDHMS));
        viewHolder.timeTextView.setText(blotter.getCreateDate().substring(blotter.getCreateDate().length() - 8, blotter.getCreateDate().length()));
        viewHolder.priceTextView.setText(blotter.getPrice());
        viewHolder.payTypeTextView.setText(blotter.getReceTypeDesc());

        viewHolder.rootView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (onItemClickListener != null) {
                    onItemClickListener.onItemClick(v, position);
                }
            }
        });
    }

    private class ContentViewHolder extends RecyclerView.ViewHolder {
        View rootView;

        ImageView payTypeImageView;
        TextView dateTextView;
        TextView timeTextView;

        TextView priceTextView;
        TextView payTypeTextView;

        public ContentViewHolder(View itemView) {
            super(itemView);
            rootView = itemView;
            payTypeImageView = (ImageView) itemView.findViewById(R.id.iv_blotter_pay_pic);
            dateTextView = (TextView) itemView.findViewById(R.id.tv_bloter_date);
            timeTextView = (TextView) itemView.findViewById(R.id.tv_bloter_tiem);

            priceTextView = (TextView) itemView.findViewById(R.id.tv_bloter_price);
            payTypeTextView = (TextView) itemView.findViewById(R.id.tv_bloter_pay_type);

        }
    }

    private TeamsAndHeaderAdapter.OnItemClickListener onItemClickListener;

    public void setOnItemClickListener(TeamsAndHeaderAdapter.OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

    public interface OnItemClickListener {
        void onItemClick(View view, int position);
    }

}
